import StorageUtils from "../../../../app/storage";
import Utils from "../../../../utils/index";
import DB from '../../../../app/db';

export const RwCode = {
  CAST01: 'CAST01',
  CAST02: 'CAST02',
  CAST03: 'CAST03',
  CAST04: 'CAST04'
};

export const RwCodeArray = ['CAST01', 'CAST02', 'CAST03', 'CAST04'];

export function getProprietorByOpenId(openId) {
  DB.WechatAPI.getProprietorByOpenId({openId: openId}).then(
    function (content) {
      if (content && content.id) {
        StorageUtils.updateUser({proprietorId: content.id});
        StorageUtils.setStorage('proprietor', content);
      } else {
        StorageUtils.updateUser({proprietorId: ''});
        StorageUtils.removeStorageItem('proprietor');
      }
    },
    function (error) {
      StorageUtils.updateUser({proprietorId: ''});
      StorageUtils.removeStorageItem('proprietor');
      Utils.myAlert(error);
    }
  ).catch(function (reason) {
    Utils.myAlert(reason);
  });
}

export function getInfoByOpenId(openId) {
  DB.WechatAPI.getInfoByOpenId({
    openId: openId,
  }).then(
    function (content) {
      if (content.length > 0) {
        StorageUtils.setStorage('doors', content);
      } else {
        StorageUtils.removeStorageItem('doors');
      }
    },
    function (error) {
      StorageUtils.removeStorageItem('doors');
      Utils.myAlert(error);
    }
  ).catch(function (reason) {
    Utils.myAlert(reason);
  });
}
